<template>
    <div>
        <h1>日志列表</h1>
        <div>
            <el-form :inline="true" :model="searhForm" class="demo-form-inline">
                <el-form-item label="关键字">
                    <el-input
                        v-model="searhForm.keyWord"
                        placeholder="请输入关键字"
                        clearable
                    />
                </el-form-item>
                <el-form-item label="日志类型">
                    <el-select
                        v-model="searhForm.logType"
                        placeholder="请选择日志类型"
                        style="width: 150px"
                        clearable
                    >
                        <el-option label="登录" :value="1" />
                        <el-option label="商品审核" :value="6" />
                    </el-select>
                </el-form-item>
                <el-form-item label="开始时间">
                    <el-date-picker
                        v-model="searhForm.beginTime"
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="选择开始时间"
                    />
                </el-form-item>
                <el-form-item label="结束时间">
                    <el-date-picker
                        v-model="searhForm.endTime"
                        type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss"
                        placeholder="选择结束时间"
                    />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="onSubmitQuery"
                        >查询</el-button
                    >
                </el-form-item>
            </el-form>
        </div>
        <div>
            <el-table :data="tableData" stripe style="width: 100%">
                <el-table-column prop="logId" label="ID" width="100" />
                <el-table-column prop="createTime" label="时间" width="180" />
                <el-table-column prop="optName" label="操作人" width="120" />
                <el-table-column prop="ip" label="IP" width="120" />
                <el-table-column prop="logName" label="名称" width="120" />
                <el-table-column prop="logType" label="类型" width="120" />
                <el-table-column prop="logContent" label="内容" width="380">
                    <template #default="scope">
                        <span v-html="scope.row.logContent"></span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" />
            </el-table>
        </div>
        <div style="margin-top: 20px">
            <el-pagination
                v-model:current-page="searhForm.pageNum"
                v-model:page-size="searhForm.pageSize"
                :page-sizes="[2, 4, 6, 8]"
                :background="true"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { getLogList } from "@/api/api";
import { onMounted } from "vue";
import { before } from "node:test";

const tableData = ref([]);
const total = ref(0);
const searhForm = ref({
    pageNum: 1,
    pageSize: 2,
    keyWord: null,
    beginTime: null,
    endTime: null,
    logType: null,
});

// 搜索
const onSubmitQuery = () => {
    console.log("onSubmitQuery", searhForm.value);
    loadList();
};

const handleSizeChange = (val: number) => {
    console.log(`${val} items per page`);
    searhForm.value.pageSize = val;
    loadList();
};
const handleCurrentChange = (val: number) => {
    console.log(`current page: ${val}`);
    searhForm.value.pageNum = val;
    loadList();
};

// 分页获取日志列表（数据是从ES中获取的）
const loadList = () => {
    getLogList(searhForm.value).then((res) => {
        console.log("loadList", res);
        if (res.data.code == 200) {
            tableData.value = res.data.data.records;
            total.value = res.data.data.total;
        }
    });
};

// 页面钩子函数中
onMounted(() => {
    loadList();
});
</script>

<style scoped></style>
